package com.htudata.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.home.mapper.MntEqpMapper;
import com.htudata.home.service.IMntEqpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MntEqpServiceImpl implements IMntEqpService {

  @Autowired
  private MntEqpMapper mntEqpMapper;

  @Override
  public JSONObject selectMntEqpData()  throws Exception {
    JSONObject jsonObject = new JSONObject();
    JSONArray eqpOverview = selectMntEqpOverview();
    JSONArray eqpStatis = selectMntEqpStatis();
    JSONArray eqpDataStatis = selectMntEqpDataStatis();
    List<JSONObject> eqpDataStatisList = null;
    Map<String, List<JSONObject>> eqpDataStatisMap = null;
    if (eqpDataStatis != null) {
      eqpDataStatisList = (List<JSONObject>)JSON.parse(eqpDataStatis.toJSONString());
      eqpDataStatisMap = eqpDataStatisList.stream().collect(Collectors.groupingBy(x -> x.getString("mntEqp")));
    } 
    jsonObject.put("eqpOverview", eqpOverview);
    jsonObject.put("eqpStatis", eqpStatis);
    jsonObject.put("eqpDataStatis", eqpDataStatisMap);
    return jsonObject;
  }

  @Override
  public JSONArray selectHisExtre()  throws Exception{
    return this.mntEqpMapper.selectHisExtre();
  }

  @Override
  public String selectMntEqpDataJSON() throws Exception {
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_009.getCode(), selectMntEqpData());
    } catch (Exception e) {
       throw e;
    } 
    return JSON.toJSONString(result);
  }

  @Override
  public String selectHisExtreJSON()  throws Exception{
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_010.getCode(), selectHisExtre());
    } catch (Exception e) {
      throw e;
    } 
    return JSON.toJSONString(result);
  }

  @Override
  public JSONArray selectMntEqpOverview()  throws Exception{
    try {
      return this.mntEqpMapper.selectMntEqpOverview();
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectMntEqpStatis()  throws Exception{
    try {
      return this.mntEqpMapper.selectMntEqpStatis();
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectMntEqpDataStatis()  throws Exception{
    try {
      return this.mntEqpMapper.selectMntEqpDataStatis();
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectStationStatus(String type, String areaCode, int status, long userId)  throws Exception {
    return this.mntEqpMapper.selectStationStatus(type, areaCode, status, userId);
  }

  @Override
  public String selectStationStatusJSON(String type, String areaCode, int status, long userId)  throws Exception {
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_019.getCode(), selectStationStatus(type, areaCode, status, userId));
    } catch (Exception e) {
      throw e;
    } 
    return JSON.toJSONString(result);
  }

  @Override
  public String changeUserStation(long userId, String stationCode, String flag)  throws Exception{
    int result = -1;
    if ("select".equalsIgnoreCase(flag)) {
      result = this.mntEqpMapper.insertUserStation(userId, stationCode);
    } else if ("unSelect".equalsIgnoreCase(flag)) {
      result = this.mntEqpMapper.deleteUserStation(userId, stationCode);
    } 
    return (result > 0) ? "success" : "falil";
  }
}
